﻿#include "fulldiskform.h"
#include <math.h>

FullDiskForm::FullDiskForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FullDiskForm)
{
    ui->setupUi(this);
    this->setObjectName("WidgetObjectFullDiskForm-FulldiskNormal");
    this->setWindowTitle(tr("FullDiskForm"));

    ui->tableWidget->setAutoFillBackground(true);
    ui->doubleSpinBox_Cycle->setValue(LoopTime);
    ui->doubleSpinBox_ScanSpeed->setValue(TaskManager::getInstance()->currentInstrument()->mdMirrorScanSpeed);
    ui->doubleSpinBox_EWMax->setValue(TaskManager::getInstance()->currentInstrument()->mdFulldisEWMax);
    ui->doubleSpinBox_EWMin->setValue(TaskManager::getInstance()->currentInstrument()->mdFulldisEWMin);
    ui->doubleSpinBox_NSMax->setValue(TaskManager::getInstance()->currentInstrument()->mdFulldisNSMax);
    ui->doubleSpinBox_NSMin->setValue(TaskManager::getInstance()->currentInstrument()->mdFulldisNSMin);
}

FullDiskForm::~FullDiskForm()
{
    delete ui;
}

void FullDiskForm::generateTask(TaskItem *pTask)
{
    TaskItem * pCurTask = NULL;
    if(!msCurTaskName.isEmpty() && NULL == pTask)// 更新任务
    {
        pCurTask = TaskManager::getInstance()->getTask(msCurTaskName);
    }
    else
    {

        pCurTask = pTask;
    }

    CommandItem::FastTargetPos fastpos;
    for(int i=0; i<ui->tableWidget->rowCount(); ++i)
    {
        CommandItem* pCommandItem = pCurTask->getCommand(ui->tableWidget->item(i,0)->data(Qt::WhatsThisRole).toString());
        if(pCommandItem == NULL)
        {
            pCommandItem = pCurTask->addCommand(ui->tableWidget->item(i,0)->text());
            pCommandItem->setWorkMode(CommandItem::Mode_SlideScan);
        }

        fastpos.miTargetPosWorkMode="wait";

        fastpos.mdTargetPosDuration=FulldiskstableTime;
        pCommandItem->setFastPosMode(fastpos);
        pCommandItem->setValue(ScriptItem::Pos_realEWstart,ui->tableWidget->item(i,0)->data(Qt::UserRole).toDouble());
        pCommandItem->setValue(ScriptItem::Pos_realEWend,ui->tableWidget->item(i,0)->data(Qt::UserRole+1).toDouble());
        pCommandItem->setValue(ScriptItem::Pos_EWStart,ui->tableWidget->item(i,1)->text().toDouble());
        pCommandItem->setValue(ScriptItem::Pos_EWEnd,ui->tableWidget->item(i,2)->text().toDouble());
        pCommandItem->setValue(ScriptItem::Pos_NSStart,ui->tableWidget->item(i,3)->text().toDouble());
        pCommandItem->setValue(ScriptItem::Pos_NSEnd,ui->tableWidget->item(i,4)->text().toDouble());
        pCommandItem->setValue(ScriptItem::Time_Start,ui->tableWidget->item(i,5)->text().toDouble());
        pCommandItem->setValue(ScriptItem::Time_End,ui->tableWidget->item(i,7)->text().toDouble());
    }
}

void FullDiskForm::setCurrentTask(const QString &sName)
{
    msCurTaskName = sName;
    ui->lineEdit_CurTask->setText(sName);
    TaskEditor::setCurrentTask(sName);
}
void FullDiskForm::setCurrentMode(int nMode)
{
    // 不属于添加模式，部分按钮应该隐藏
    if(nMode != 0)
    {
        ui->doubleSpinBox_EWMax->setEnabled(false);
        ui->doubleSpinBox_EWMin->setEnabled(false);
        ui->doubleSpinBox_NSMax->setEnabled(false);
        ui->doubleSpinBox_NSMin->setEnabled(false);
    }

    TaskEditor::setCurrentMode(nMode);
}

void FullDiskForm::updateTask(TaskItem *pTask)
{
    if(NULL == pTask)
        return;
    msCurTaskName = pTask->objectName();

    QVector<CommandItem *> vctCmds = pTask->allCommands();
    ui->tableWidget->setRowCount(vctCmds.count());
    for(int i=0; i<vctCmds.count(); ++i){
        QTableWidgetItem* pTableItem = new QTableWidgetItem(vctCmds.at(i)->getName());
        // 设置对象名称，为了在计算的时候不重新添加，同时可在更新的时候方便查找
        pTableItem->setData(Qt::WhatsThisRole,vctCmds.at(i)->objectName());

        ui->tableWidget->setItem(i,0,pTableItem);
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(QString::number(vctCmds.at(i)->getValue(ScriptItem::Pos_EWStart),'f',6)));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(QString::number(vctCmds.at(i)->getValue(ScriptItem::Pos_EWEnd),'f',6)));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(QString::number(vctCmds.at(i)->getValue(ScriptItem::Pos_NSStart),'f',6)));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(vctCmds.at(i)->getValue(ScriptItem::Pos_NSEnd),'f',6)));

        double dTimeStart = vctCmds.at(i)->getValue(ScriptItem::Time_Start);
        double dTimeEnd = vctCmds.at(i)->getValue(ScriptItem::Time_End);

        ui->tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(dTimeStart,'f',6)));
        ui->tableWidget->setItem(i,6,new QTableWidgetItem(QString::number((dTimeStart+dTimeEnd)/2,'f',6)));
        ui->tableWidget->setItem(i,7,new QTableWidgetItem(QString::number(dTimeEnd,'f',6)));
        ui->tableWidget->setItem(i,8,new QTableWidgetItem(QString::number(dTimeEnd-dTimeStart,'f',6)));
        if(i==0)
        {
            ui->tableWidget->setCurrentItem(pTableItem);
        }
    }
}

void FullDiskForm::on_pushButton_CalPos_clicked()
{
    // 按照圆形区域，单行扫描
    double dEWMin = ui->doubleSpinBox_EWMin->value();
    double dEWMax = ui->doubleSpinBox_EWMax->value();

    double dNSMin = ui->doubleSpinBox_NSMin->value();
    double dNSMax = ui->doubleSpinBox_NSMax->value();

    double dMirrHeight = TaskManager::getInstance()->currentInstrument()->mdMirrorHeight;
    //    double dMirrLeftHalf = TaskManager::getInstance()->currentInstrument()->mdMirrorLeftHalf;
    //    double dMirrRightHalf = TaskManager::getInstance()->currentInstrument()->mdMirrorRightHalf;

    double rlx[2]={0};


    //实际开始和结束距离
    double predistance=0.0;
    double afterdistance=0.0;

    //getActuallocation(dEWMin,dEWMax,rlx);
    double ewstart=0.0;
    double ewend=0.0;
    double nsstart=0.0;
    double nsend=0.0;
    double dTotTime=0.0;

    int ScanRowNumber = ceil((dNSMax-dNSMin) / dMirrHeight);
    if(ui->comboBox_RegionMode->currentIndex() > 1)// 往返扫描模式
    {
        ScanRowNumber = ScanRowNumber%2 == 0? ScanRowNumber:ScanRowNumber+1;
    }
    double dAlighTime = ui->doubleSpinBox_align->value();

    // 圆形
    if(ui->comboBox_RegionMode->currentIndex() == 0)
    {
        double dCircleR = qMax(dNSMax-dNSMin,dEWMax-dEWMin) / 2.0;// 圆形半径
        for(int i=0; i<ScanRowNumber; ++i)
        {
            //中间对齐
            if(ui->comboBox_PosAlign->currentIndex() == 0)
            {
                //奇数行
                if(ScanRowNumber%2)
                {
                    nsstart=((ScanRowNumber-1)/2-i)*dMirrHeight;
                }
                else
                {
                    nsstart=(ScanRowNumber/2-i)*dMirrHeight-dMirrHeight/2;

                }
                nsend=nsstart;
            }
            // 大数对齐，北侧，正向
            else if(ui->comboBox_PosAlign->currentIndex() == 1)
            {
                nsstart=dCircleR-dMirrHeight*0.5-dMirrHeight*i;
                //nsstart=dNSMax-dMirrHeight*0.5-dMirrHeight*i;
                nsend=nsstart;
                //dNSMin = dNSMax - dRealHeight;
            }
            // 小数对齐
            else
            {
                //(总行号-行号-1)镜子高度-半个镜子高度-半径
                nsstart=(ScanRowNumber-i)*dMirrHeight-dMirrHeight/2-dCircleR;
                //nsstart=dNSMin+(ScanRowNumber-i)*dMirrHeight-dMirrHeight/2;
                nsend=nsstart;
                //dNSMax = dNSMin + dRealHeight;
            }
            if(nsstart>0)
                ewend=sqrt(dCircleR*dCircleR-(nsstart-dMirrHeight/2)*(nsstart-dMirrHeight/2));
            else
            {
                ewend=sqrt(dCircleR*dCircleR-(nsstart+dMirrHeight/2)*(nsstart+dMirrHeight/2));
            }
            ewstart=0-ewend;

            predistance=ewstart;
            afterdistance=ewend;

            TaskManager::getInstance()->currentInstrument()->getActuallocation(ewstart,ewend,rlx,ui->doubleSpinBox_ScanSpeed->text().toDouble());

            ewstart=rlx[0];
            ewend=rlx[1];
            dTotTime=TaskManager::getInstance()->currentInstrument()->getScanTime(ewstart,ewend,ui->doubleSpinBox_ScanSpeed->text().toDouble());

            //如果是添加模式需要将位置填好，否则用之前的值
            if(mnWorkMode==0)
            {
                ui->tableWidget->setRowCount(ScanRowNumber);
                QTableWidgetItem* pNameItem = new QTableWidgetItem(tr("%1 %2/%3").arg(msCurTaskType).arg(i + 1).arg(ScanRowNumber));
                pNameItem->setData(Qt::WhatsThisRole, QString("none"));
                ui->tableWidget->setItem(i,0, pNameItem);
                ui->tableWidget->item(i,0)->setData(Qt::UserRole,predistance);
                ui->tableWidget->item(i,0)->setData(Qt::UserRole+1,afterdistance);

                ui->tableWidget->setItem(i,1,new QTableWidgetItem(QString::number(ewstart,'f',6)));
                ui->tableWidget->setItem(i,2,new QTableWidgetItem(QString::number(ewend,'f',6)));
                ui->tableWidget->setItem(i,3,new QTableWidgetItem(QString::number(nsstart,'f',6)));
                ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(nsend,'f',6)));
            }
            //  中间时间对齐
            if(ui->comboBox_TimeAlign->currentIndex() == 0)
            {
                if(mnWorkMode==0)
                {
                    ui->tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(dAlighTime - dTotTime*0.5,'f',6)));
                    ui->tableWidget->setItem(i,6,new QTableWidgetItem(QString::number(dAlighTime,'f',6)));
                    ui->tableWidget->setItem(i,7,new QTableWidgetItem(QString::number(dAlighTime + dTotTime*0.5,'f',6)));
                    ui->tableWidget->setItem(i,8,new QTableWidgetItem(QString::number(dTotTime,'f',6)));

                }
                else
                {
                    ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
                    ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime,'f',6));
                    ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
                    ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
                }
            }
            // 开始时间对齐
            else if(ui->comboBox_TimeAlign->currentIndex() == 1)
            {
                if(mnWorkMode==0)
                {
                    ui->tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(dAlighTime,'f',6)));
                    ui->tableWidget->setItem(i,6,new QTableWidgetItem(QString::number(dAlighTime + dTotTime*0.5,'f',6)));
                    ui->tableWidget->setItem(i,7,new QTableWidgetItem(QString::number(dAlighTime + dTotTime,'f',6)));
                    ui->tableWidget->setItem(i,8,new QTableWidgetItem(QString::number(dTotTime,'f',6)));
                }
                else
                {
                    ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime,'f',6));
                    ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
                    ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime + dTotTime,'f',6));
                    ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
                }
            }
            // 结束时间对齐
            else
            {
                if(mnWorkMode==0)
                {
                    ui->tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(dAlighTime - dTotTime,'f',6)));
                    ui->tableWidget->setItem(i,6,new QTableWidgetItem(QString::number(dAlighTime - dTotTime*0.5,'f',6)));
                    ui->tableWidget->setItem(i,7,new QTableWidgetItem(QString::number(dAlighTime,'f',6)));
                    ui->tableWidget->setItem(i,8,new QTableWidgetItem(QString::number(dTotTime,'f',6)));
                }
                else
                {
                    ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime - dTotTime,'f',6));
                    ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
                    ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime,'f',6));
                    ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
                }
            }

            dAlighTime += ui->doubleSpinBox_Cycle->value();

        }

    }


    on_comboBox_TimeAlign_currentIndexChanged(ui->comboBox_TimeAlign->currentIndex());

}


void FullDiskForm::on_pushButton_AddCycle_clicked()
{
    if (ui->tableWidget->currentRow() < 0)
        return;
    for(int i=ui->tableWidget->currentRow(); i<ui->tableWidget->rowCount(); ++i)
    {
        double dTotTime =  ui->tableWidget->item(i,8)->text().toDouble();
        //  中间时间对齐
        if(ui->comboBox_TimeAlign->currentIndex() == 0)
        {
            double dAlighTime =ui->tableWidget->item(i,6)->text().toDouble() + ui->doubleSpinBox_Cycle->value();

            ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
        // 开始时间对齐
        else if(ui->comboBox_TimeAlign->currentIndex() == 1)
        {
            double dAlighTime =ui->tableWidget->item(i,5)->text().toDouble() + ui->doubleSpinBox_Cycle->value();

            ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime + dTotTime,'f',6));
            ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
        // 结束时间对齐
        else
        {
            double dAlighTime =ui->tableWidget->item(i,7)->text().toDouble() + ui->doubleSpinBox_Cycle->value();

            ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime - dTotTime,'f',6));
            ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
    }
}

void FullDiskForm::on_pushButton_DeCycle_clicked()
{
    for(int i=ui->tableWidget->currentRow(); i>=0; --i)
    {
        double dTotTime =  ui->tableWidget->item(i,8)->text().toDouble();
        //  中间时间对齐
        if(ui->comboBox_TimeAlign->currentIndex() == 0)
        {
            double dAlighTime =ui->tableWidget->item(i,6)->text().toDouble() - ui->doubleSpinBox_Cycle->value();

            ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
        // 开始时间对齐
        else if(ui->comboBox_TimeAlign->currentIndex() == 1)
        {
            double dAlighTime =ui->tableWidget->item(i,5)->text().toDouble() - ui->doubleSpinBox_Cycle->value();

            ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime + dTotTime,'f',6));
            ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
        // 结束时间对齐
        else
        {
            double dAlighTime =ui->tableWidget->item(i,7)->text().toDouble() - ui->doubleSpinBox_Cycle->value();

            ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime - dTotTime,'f',6));
            ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
    }
}

void FullDiskForm::on_comboBox_TimeAlign_currentIndexChanged(int index)
{
    int AlignCol = -1;
    // 中间时间对齐
    if(index == 0)
    {
        AlignCol = 6;
    }
    // 开始时间对齐
    else if(index == 1)
    {
        AlignCol = 5;
    }
    // 结束时间对齐
    else
    {
        AlignCol = 7;
    }

    int iCurRow = ui->tableWidget->currentRow();
    if(iCurRow < 0)
        iCurRow = 0;

    ui->tableWidget->setCurrentCell(iCurRow,AlignCol);

    for(int i=0; i<ui->tableWidget->rowCount(); ++i)
    {
        for(int j=0; j<ui->tableWidget->columnCount(); ++j)
        {
            QTableWidgetItem* pItem = ui->tableWidget->item(i,j);
            if(pItem == NULL)
                continue;

            if(i == iCurRow && j == AlignCol)
            {
                pItem->setIcon(QIcon(QPixmap(":/image/res/redflag.png")));
            }
            else
            {
                pItem->setIcon(QIcon());
            }
        }
    }

}

void FullDiskForm::on_tableWidget_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
{
    int AlignCol = -1;
    // 中间时间对齐
    if(ui->comboBox_TimeAlign->currentIndex() == 0)
    {
        AlignCol = 6;
    }
    // 开始时间对齐
    else if(ui->comboBox_TimeAlign->currentIndex() == 1)
    {
        AlignCol = 5;
    }
    // 结束时间对齐
    else
    {
        AlignCol = 7;
    }

    if(ui->tableWidget->item(previousRow,AlignCol) != NULL)
        ui->tableWidget->item(previousRow,AlignCol)->setIcon(QIcon());

    if(NULL != ui->tableWidget->item(currentRow,AlignCol))
        ui->tableWidget->item(currentRow,AlignCol)->setIcon(QIcon(QPixmap(":/image/res/redflag.png")));
}
